package com.xin.lv.yang.utils.utils;

import android.content.Context;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DataUtil {

    /**
     * 时间戳转成时间
     *
     * @param time 时间戳
     * @param str  格式
     * @return 转换的时间
     */
    public String longTimeToString(long time, String str) {
        SimpleDateFormat format = new SimpleDateFormat(str, Locale.US);
        return format.format(new Date(time));
    }

    /**
     * 获取今天往后一周的日期(几月几号)
     */
    public ArrayList getSevendate() {
        ArrayList dates = new ArrayList();
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));

        for (int i = 0; i < 7; i++) {
            String mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
            String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
            String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + i);// 获取当前日份的日期号码
            String date = mMonth + "月" + mDay + "日";
            dates.add(date);
        }
        return dates;
    }

    /**
     * 获取今天往后一周的日期(几月几号)
     */
    public ArrayList getTime() {
        ArrayList dates = new ArrayList();
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));

        for (int i = 0; i < 7; i++) {
            String mYear = String.valueOf(c.get(Calendar.YEAR));// 获取当前年份
            String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
            String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + i);// 获取当前日份的日期号码
            String date;
            if (mMonth.equals("10")) {
                date = mYear + "-" + mMonth + "-" + mDay;
            } else if (mMonth.equals("11")) {
                date = mYear + "-" + mMonth + "-" + mDay;
            } else if (mMonth.equals("12")) {
                date = mYear + "-" + mMonth + "-" + mDay;
            } else {
                date = mYear + "-0" + mMonth + "-" + mDay;
            }

            dates.add(date);
        }
        return dates;
    }

    /**
     * 获取周几
     *
     * @param strDate 时间字符串
     * @return 周几字符串
     */
    public String getWeekByDateStr(String strDate, String type) {
        String[] ss = strDate.split(type);
        int year = Integer.parseInt(ss[0]);
        int month = Integer.parseInt(ss[1]);
        int day = Integer.parseInt(ss[2]);

        Calendar c = Calendar.getInstance();

        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);
        c.set(Calendar.DAY_OF_MONTH, day);

        String week = "";
        int weekIndex = c.get(Calendar.DAY_OF_WEEK);

        switch (weekIndex) {
            case 1:
                week = "周日";
                break;
            case 2:
                week = "周一";
                break;
            case 3:
                week = "周二";
                break;
            case 4:
                week = "周三";
                break;
            case 5:
                week = "周四";
                break;
            case 6:
                week = "周五";
                break;
            case 7:
                week = "周六";
                break;
        }
        return week;
    }


    public static String longToTime(long l) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);

        String sss = String.valueOf(l);
        if (sss.length() == 13) {
            return format.format(new Date(l));
        } else {
            return format.format(new Date(l * 1000));
        }
    }


    public static String longToTime(long l, String type) {
        SimpleDateFormat format = new SimpleDateFormat(type, Locale.CHINA);

        return format.format(new Date(l * 1000));

    }

    public static long stringTimeToLong(String date, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, Locale.CHINA);
        try {
            Date d = simpleDateFormat.parse(date);
            return d.getTime();

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }


    private static final double EARTH_RADIUS = 6378137.0;

    /**
     * 两点间的距离
     */
    public static double gps2m(double lat_a, double lng_a, double lat_b, double lng_b) {
        double radLat1 = (lat_a * Math.PI / 180.0);
        double radLat2 = (lat_b * Math.PI / 180.0);
        double a = radLat1 - radLat2;
        double b = (lng_a - lng_b) * Math.PI / 180.0;
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }


    /**
     * 将系统表情转化为字符串
     */
    public static String getString(String s) {
        int length = s.length();

        StringBuilder sb = new StringBuilder();

        //循环遍历字符串，将字符串拆分为一个一个字符
        for (int i = 0; i < length; i++) {
            char codePoint = s.charAt(i);
            //判断字符是否是emoji表情的字符
            if (isEmojiCharacter(codePoint)) {
                // 如果是将以大括号括起来
                String emoji = "{" + Integer.toHexString(codePoint) + "}";

                sb.append(emoji);

            } else {
                sb.append(codePoint);
            }

        }
        return sb.toString();
    }

    /**
     * 是否包含表情
     */
    private static boolean isEmojiCharacter(char codePoint) {
        return !((codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA)
                || (codePoint == 0xD)
                || ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
                || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
                || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)));
    }


    /**
     * 将表情描述转换成表情
     */
    public static String getEmoji(Context context, String str) {
        String string = str;
        String rep = "\\{(.*?)\\}";
        Pattern p = Pattern.compile(rep);
        Matcher m = p.matcher(string);
        while (m.find()) {
            String s1 = m.group().toString();
            String s2 = s1.substring(1, s1.length() - 1);
            String s3;
            try {
                s3 = String.valueOf((char) Integer.parseInt(s2, 16));
                string = string.replace(s1, s3);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return string;
    }


}
